package com.ruoyi.xiaoshouchuku.controller;

import com.ruoyi.xiaoshouchuku.domain.XiaoshouChuku;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

public class chuKuDanUpload {

    private static final String filePath = "C:/ruoyi/uploadPath/upload/ChuKuDan/";
    //private static String loadPath="";

    public static void img2New(XiaoshouChuku xiaoshouChuku){
        int width = 905; // 图片宽
        int height = 343;// 图片高
        String titleStr = "现货仓发货通知单";
        String fahuodanhaoName = "发货单号";
        String fahuodanhao = xiaoshouChuku.getFahuoDanhao();
        String xiangmumingName = "项目名称";
        String xiangmumingcheng = xiaoshouChuku.getXiangmuName();
        String yewuyuanName = "业务员";
        String yewuyuan = xiaoshouChuku.getXiangmuMen();
        String fahuorenName = "发货人";
        String fahuoren = xiaoshouChuku.getFahuoRen();
        String riqiName = "日期";
        String riqi = new SimpleDateFormat("yyyy.MM.dd").format(xiaoshouChuku.getChukuDate());
        String shouhuodizhiName = "收货地址";
        String shouhuodizhi = xiaoshouChuku.getShouhuoDizhi();
        String xiaoshoueName = "销售额 元";
        String xiaoshoue = String.valueOf(xiaoshouChuku.getZhonjine());
        String pinmingName = "品名",chicunyukaixiangName="产品尺寸及开向",chanpinshuliangName="产品数量",chanpinbeizhuName="产品备注";
        String menming = xiaoshouChuku.getGoodNameMen1(),kuangming = xiaoshouChuku.getGoodNameKuang();
        String fanghuosuo="防火锁",bimenqi="闭门器",labaogai="拉爆盖";
        String menshsangao = null,menshankuan = null,menkaixiang = null,menshanshu = null;
        if (menming != null) {
            menshsangao = String.valueOf(xiaoshouChuku.getMen1shanGao());
            menshankuan = String.valueOf(xiaoshouChuku.getMen1shanKuan());
            menkaixiang = xiaoshouChuku.getKaiXiang1();
            menshanshu = String.valueOf(xiaoshouChuku.getMen1Shuliang());
        }else if (Objects.equals(menming, "") && kuangming != null){
            menming = kuangming;
            menshsangao = String.valueOf(xiaoshouChuku.getKuangGao());
            menshankuan = String.valueOf(xiaoshouChuku.getKuangKuan());
            menkaixiang = xiaoshouChuku.getKaiXiang2();
            menshanshu = String.valueOf(xiaoshouChuku.getKuangShuliang());
        }
        String menkuanshu = String.valueOf(xiaoshouChuku.getKuangShuliang());
        String wujin1shu = xiaoshouChuku.getShuliang1();
        String wujin2shu = xiaoshouChuku.getShuliang2();
        String wujin3shu = xiaoshouChuku.getShuliang3();
        String menbeizhu = xiaoshouChuku.getBeizhu1();

        // 得到图片缓冲区
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// INT精确度达到一定,RGB三原色，高度70,宽度150

        // 得到它的绘制环境(这张图片的笔)
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setColor(Color.WHITE); // 设置背景颜色
        g2.fillRect(0, 0, width, height);// 填充整张图片(其实就是设置背景颜色)
        g2.setColor(Color.black);// 设置字体颜色
        g2.setStroke(new BasicStroke(2.0f)); // 边框加粗
        g2.drawRect(1, 1, width - 2, height - 2); // 画边框就是黑边框

        g2.drawLine(0, 37, 905, 37); // 从上到下第二个横线(标题下面横线)
        g2.setStroke(new BasicStroke(0.0f)); // 边框不需要加粗
        g2.drawLine(0, 76, 905, 76); // 从上到下第三个横线(账单周期下面横线)
        g2.drawLine(0, 114, 905, 114); // 从上到下第四个横线(账单天数下面横线)
        g2.drawLine(0, 152, 905, 152); // 从上到下第5个横线(泵额定功率下面横线)
        g2.drawLine(0, 190, 905, 190); // 从上到下第6个横线(泵变频节能运行用电量下面横线)
        g2.drawLine(0, 228, 905, 228); // 从上到下第7个横线(电价下面横线)
        g2.drawLine(0, 266, 905, 266); // 从上到下第8个横线(平均节能率下面横线)
        g2.drawLine(0, 304, 905, 304);// 从上到下第9个横线(平均节能率下面横线)

        g2.drawLine(122, 38, 122, 343); // 从左到右第二个竖线
        g2.drawLine(224, 190, 224, 343); // 从左到右第三个竖线
        g2.drawLine(240, 190, 240, 343); // 从左到右第四个竖线
        g2.drawLine(320, 38, 320, 114); // 从左到右第5个竖线
        g2.drawLine(320, 190, 320, 343); // 从左到右第5个竖线
        g2.drawLine(490, 38, 490, 114); // 从左到右第6个竖线
        g2.drawLine(490, 152, 490, 343); // 从左到右第6个竖线
        g2.drawLine(630, 38, 630, 343); // 从左到右第7个竖线
        g2.drawLine(720, 76, 720, 152); // 从左到右第8个竖线

        // 设置标题的字体,字号,大小
        Font titleFont = new Font("宋体", Font.BOLD, 30);
        g2.setFont(titleFont);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 抗锯齿
        // 计算文字长度,计算居中的X点坐标
        FontMetrics fm = g2.getFontMetrics(titleFont);
        int titleWidth = fm.stringWidth(titleStr);
        int titleWidthX = (width - titleWidth) / 2;// 感觉不居中,向左移动35个单位
        g2.drawString(titleStr, titleWidthX, 30);

            //发货单号
            g2.setFont(new Font("宋体", Font.BOLD, 18));
            g2.drawString(fahuodanhaoName, 15, 65);
            // 发货单号的值
            g2.drawString(fahuodanhao, 130, 65);

        //发货单号
        g2.drawString(xiangmumingName, 520, 65);
        // 发货单号的值
        g2.drawString(xiangmumingcheng, 635, 65);

        //发货单号
        g2.drawString(yewuyuanName, 30, 100);
        // 发货单号的值
        g2.drawString(yewuyuan, 185, 100);


        //发货单号
        g2.drawString(fahuorenName, 370, 100);
        // 发货单号的值
        g2.drawString(fahuoren, 525, 100);
        //发货单号
        g2.drawString(riqiName, 654, 103);
        // 发货单号的值
        g2.drawString(riqi, 735, 100);

        //发货单号
        g2.drawString(shouhuodizhiName, 17, 140);
        // 发货单号的值
        g2.setFont(new Font("宋体", Font.BOLD, 20));
        g2.drawString(shouhuodizhi, 130, 140);

        //发货单号
        g2.setFont(new Font("宋体", Font.BOLD, 18));
        g2.drawString(xiaoshoueName, 632, 140);
        // 发货单号的值
        g2.drawString(xiaoshoue, 735, 140);

        //产品详细
        g2.drawString(pinmingName, 37, 178);
        g2.drawString(chicunyukaixiangName, 230, 178);
        g2.drawString(chanpinshuliangName, 522, 178);
        g2.drawString(chanpinbeizhuName, 725, 178);

        //门详细
        if (menming == null) {
            g2.drawString(kuangming, 7, 215);
            g2.drawString(xiaoshouChuku.getKuangKuan(), 180, 215);
            g2.drawString("*", 228, 215);
            g2.drawString(xiaoshouChuku.getKuangGao(), 245, 215);
            g2.drawString(xiaoshouChuku.getKaiXiang2(), 330, 215);
            g2.drawString(menkuanshu, 550, 215);
        }else {
            g2.drawString(menming, 7, 215);
            g2.drawString(menshankuan, 180, 215);
            g2.drawString("*", 228, 215);
            g2.drawString(menshsangao, 245, 215);
            g2.drawString(menkaixiang, 330, 215);
            g2.drawString(menshanshu, 550, 215);
            g2.drawString(menbeizhu, 730, 215);
        }

        //其他商品
        g2.drawString(fanghuosuo, 30, 255);
        g2.drawString(bimenqi, 30, 292);
        g2.drawString(labaogai, 30, 330);

        //其他商品
        g2.drawString(wujin1shu, 550, 255);
        g2.drawString(wujin2shu, 550, 292);
        g2.drawString(wujin3shu, 550, 330);

        g2.dispose(); // 释放对象
        try {
            //ImageIO.write(bi, "JPEG", new FileOutputStream("D:/a.jpg"));// 保存图片 JPEG表示保存格式
            ImageIO.write(bi, "JPEG", new FileOutputStream(filePath+xiaoshouChuku.getChukuDanhao()+".jpg"));// 保存图片 JPEG表示保存格式
            //xiaoshouChukuMapper.insertXiaoshouChukuIMGPath(xiaoshouChuku.getId(),loadPath+xiaoshouChuku.getChukuDanhao()+".jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

        public static void main(String[] args) throws IOException {

        XiaoshouChuku xiaoshouChuku = new XiaoshouChuku();
//        xiaoshouChuku.setChukuDanhao("00000001");
//        xiaoshouChuku.setFahuoDanhao("MDQ20230516-906");
//        xiaoshouChuku.setXiangmuName("广西平南华鑫");
//        xiaoshouChuku.setXiangmuMen("李松林");
//        xiaoshouChuku.setFahuoRen("吴悠/张凤玲");
//            try {
//                xiaoshouChuku.setChukuDate(new SimpleDateFormat("yyyy-MM-dd").parse("2023-05-16"));
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//            xiaoshouChuku.setShouhuoDizhi("物流提付:广西平南镇莫总18074889313");
//        xiaoshouChuku.setZhonjine(1900L);
//        xiaoshouChuku.setGoodNameMen("钢制防火门");
//        xiaoshouChuku.setGoodNameKuang("钢制防火门");
//        xiaoshouChuku.setKaiXiang("外双开");
//        xiaoshouChuku.setMenShuliang(new BigDecimal(2));
//        xiaoshouChuku.setKuangShuliang(String.valueOf(new BigDecimal(2)));
//        xiaoshouChuku.setMenshanKuan(1180L);
//        xiaoshouChuku.setMenshanGao(2080L);
//        xiaoshouChuku.setShuliang1("2");
//        xiaoshouChuku.setShuliang2("4");
//        xiaoshouChuku.setShuliang3("12");
//        xiaoshouChuku.setBeizhu1("甲级");

            img2New(xiaoshouChuku);
        }
}
